package com.alibaba.algo.controller.user;

import com.alibaba.algo.apiimpl.user.SysResourceServiceImpl;
import com.alibaba.algo.cooperation.usercomponent.vo.req.user.ResourceActionRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.req.user.ResourceRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.req.user.SelectResourceListRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.req.xml.ImportParam;
import com.alibaba.algo.core.base.PagerEntity;
import com.alibaba.algo.entity.user.SysResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import com.alibaba.algo.cooperation.common.BaseResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 类SysResourceController.java的实现描述：资源 Controller
 *
 * @author nieganggang 2021-03-12 16:08:17
 */
@RestController
@RequestMapping(value = "/usercenter-admin-app/admin/sysResource")
public class SysResourceController {

    @Autowired
    SysResourceServiceImpl sysResourceService;

    @PostMapping("/selectPage")
    public BaseResult<PagerEntity<SysResource>> selectPage(@RequestBody SelectResourceListRequestVO vo) {
        if (ObjectUtils.isEmpty(vo.getPageIndex()) || ObjectUtils.isEmpty(vo.getPageSize())) {
            return BaseResult.buildError("分页信息为空");
        }
        return sysResourceService.selectPage(vo);
    }

    @RequestMapping(value = "/selectResourceAction", method = RequestMethod.POST)
    BaseResult selectResourceAction(@RequestBody SelectResourceListRequestVO vo) {
        return sysResourceService.selectResourceAction(vo.getResourceId(), vo.getAppCode());
    }

    @RequestMapping(value = "/updateResourceAction", method = RequestMethod.POST)
    BaseResult updateResourceAction(@RequestBody ResourceActionRequestVO vo) {
        return sysResourceService.updateResourceAction(vo);
    }

    @RequestMapping(value = "/removeResourceAction", method = RequestMethod.POST)
    BaseResult removeResourceAction(@RequestBody ResourceActionRequestVO vo) {
        return sysResourceService.removeResourceAction(vo);
    }

    @RequestMapping(value = "/removeResource", method = RequestMethod.POST)
    BaseResult removeResourceAction(@RequestBody ResourceRequestVO vo) {
        return sysResourceService.removeResource(vo);
    }

    @RequestMapping(value = "/export", method = RequestMethod.POST)
    void resourceExport(HttpServletResponse response, @RequestBody ImportParam param) {
        sysResourceService.resourceExport(response, param);
    }

    @RequestMapping(value = "/import", method = RequestMethod.POST)
    BaseResult resourceImport(@RequestBody MultipartFile file, @RequestParam("appCode") String appCode) {
        return sysResourceService.resourceImport(file, appCode);
    }

    @GetMapping(value = "/qryResourceRoot")
    BaseResult<SysResource> qryResourceRoot(@RequestParam("appCode") String appCode) {
        return sysResourceService.qryResourceRoot(appCode);
    }

    @PostMapping(value = "/getChildResourceListByParentId")
    BaseResult<List<SysResource>> getChildResourceListByParentId(@RequestBody SelectResourceListRequestVO param) {
        return sysResourceService.getChildResourceListByParentId(param);
    }

    @PostMapping(value = "/save")
    BaseResult<SysResource> save(@RequestBody SelectResourceListRequestVO param) {
        return sysResourceService.save(param);
    }
}
